package com.putao.tinytime.util;

import android.content.Context;
import android.database.Cursor;
import android.provider.MediaStore;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.ListIterator;
import java.util.Map;

import com.putao.tinytime.bean.GalleryEntity;
import com.putao.tinytime.bean.PhotoGridItem;
import com.putao.tinytime.constants.PuTaoConstants;

/**
 * Created by jidongdong on 15/2/3.
 */
public class PhotoUtil {

    public static ArrayList<PhotoGridItem> QueryALLPhoto(Context context) {
        Map<String, Integer> sectionMap = new HashMap<String, Integer>();
        ArrayList<PhotoGridItem> result = new ArrayList<PhotoGridItem>();
        int section = 1;
        //需要查询的字段 date(date_added,'unixepoch','localtime')
        String[] projection = new String[]{MediaStore.Images.ImageColumns.DATA, MediaStore.Images.ImageColumns.DATE_MODIFIED};
        //查询条件
        String selection = MediaStore.Images.Media.DATA + " like '%" + PuTaoConstants.PAIAPI_PHOTOS_FOLDER + "%' or " + MediaStore.Images.Media.DATA + " like '%DCIM%' ";
        //排序语句
        String sortString = MediaStore.Images.Media.DATE_MODIFIED + " desc";
        Cursor cursor = context.getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, projection, selection, null, sortString);
        if (cursor != null) {
            cursor.getCount();
            while (cursor.moveToNext()) {
                long date = cursor.getLong(cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATE_MODIFIED));
                String path = cursor.getString(cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA));
                PhotoGridItem item = new PhotoGridItem(path, DateUtil.getDate(date));
                result.add(item);
            }
        }
        cursor.close();

        for (ListIterator<PhotoGridItem> it = result.listIterator(); it.hasNext(); ) {
            PhotoGridItem mGridItem = it.next();
            String ym = mGridItem.getTime();
            if (!sectionMap.containsKey(ym)) {
                mGridItem.setSection(section);
                sectionMap.put(ym, section);
                section++;
            } else {
                mGridItem.setSection(sectionMap.get(ym));
            }
        }
        return result;
    }


    public static ArrayList<PhotoGridItem> QueryPhotoByBUCKET_ID(Context context, int bucket_id) {
        Map<String, Integer> sectionMap = new HashMap<String, Integer>();
        ArrayList<PhotoGridItem> result = new ArrayList<PhotoGridItem>();
        int section = 1;
        //需要查询的字段 date(date_added,'unixepoch','localtime')
        String[] projection = new String[]{MediaStore.Images.ImageColumns.DATA, MediaStore.Images.ImageColumns.DATE_MODIFIED, MediaStore.Images.Media.BUCKET_ID};
        //查询条件
        String selection = MediaStore.Images.Media.BUCKET_ID + " = " + bucket_id;
        //排序语句
        String sortString = MediaStore.Images.Media.DATE_MODIFIED + " desc";
        Cursor cursor = context.getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, projection, selection, null, sortString);
        if (cursor != null) {
            cursor.getCount();
            while (cursor.moveToNext()) {
                long date = cursor.getLong(cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATE_MODIFIED));
                String path = cursor.getString(cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA));
                PhotoGridItem item = new PhotoGridItem(path, DateUtil.getDate(date));
                result.add(item);
            }
        }
        cursor.close();

        for (ListIterator<PhotoGridItem> it = result.listIterator(); it.hasNext(); ) {
            PhotoGridItem mGridItem = it.next();
            String ym = mGridItem.getTime();
            if (!sectionMap.containsKey(ym)) {
                mGridItem.setSection(section);
                sectionMap.put(ym, section);
                section++;
            } else {
                mGridItem.setSection(sectionMap.get(ym));
            }
        }
        return result;
    }


    public static ArrayList<GalleryEntity> QueryALLGalleryList(Context context) {
        ArrayList<GalleryEntity> galleryList = new ArrayList<GalleryEntity>();

        String[] projection = {MediaStore.Images.Media._ID, MediaStore.Images.Media.DATA, MediaStore.Images.Media.BUCKET_ID, MediaStore.Images.Media.BUCKET_DISPLAY_NAME, "COUNT(1) AS count"};
        String selection = "0==0) GROUP BY (" + MediaStore.Images.Media.BUCKET_ID;
        String sortOrder = MediaStore.Images.Media.DATE_MODIFIED;

        Cursor cur = context.getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, projection, selection, null, sortOrder);
        if (cur.moveToFirst()) {
            int id_column = cur.getColumnIndex(MediaStore.Images.Media._ID);
            int image_id_column = cur.getColumnIndex(MediaStore.Images.Media.DATA);
            int bucket_id_column = cur.getColumnIndex(MediaStore.Images.Media.BUCKET_ID);
            int bucket_name_column = cur.getColumnIndex(MediaStore.Images.Media.BUCKET_DISPLAY_NAME);
            int count_column = cur.getColumnIndex("count");

            do {
                int id = cur.getInt(id_column);
                String image_path = cur.getString(image_id_column);
                int bucket_id = cur.getInt(bucket_id_column);
                String bucket_name = cur.getString(bucket_name_column);
                if (bucket_name.equalsIgnoreCase("watermark"))
                    continue;
                int count = cur.getInt(count_column);
                GalleryEntity gallery = new GalleryEntity();
                gallery.setId(id);
                gallery.setImage_path(image_path);
                gallery.setBucket_id(bucket_id);
                gallery.setBucket_name(bucket_name);
                gallery.setCount(count);
                galleryList.add(gallery);
            } while (cur.moveToNext());
        }
        return galleryList;
    }
}
